package cn.cloudreal.cbms.project.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import cn.cloudreal.cbms.common.exception.ITCException;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.project.dao.ProjectDao;
import cn.cloudreal.cbms.project.service.ProjectService;
import cn.cloudreal.cbms.vo.CastForProj;
import cn.cloudreal.cbms.vo.Project;

public class ProjectServiceImpl implements ProjectService
{
    private static final Logger log = Logger.getLogger(ProjectServiceImpl.class);

    private ProjectDao projectDao;

    /**
     * @category 新增项目信息
     * @exception ITCException
     * @param project
     *            Project
     * @return boolean
     */
    public boolean addProjectInfo(Project project) throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl addProjectInfo() method,return boolean");
        }

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("projectId", project.getProjectId());
        params.put("projectName", project.getProjectName());
        boolean isExist = projectDao.isProjectExist(params);
        boolean flag = false;
        if (!isExist)
        {
            flag = projectDao.addProject(project);
        }
        else
        {
            log.error("该项目信息已经存在，项目信息无法新增，项目编号： " + project.getProjectId());
            throw new ITCException("3001");
        }

        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl addProjectInfo() method,return boolean");
        }

        return flag;
    }

    /**
     * @category 修改项目信息
     * @exception ITCException
     * @param project
     *            Project
     * @return boolean
     */
    public boolean modProjectInfo(Project project) throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl modProjectInfo() method,return boolean");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("projectId", project.getProjectId());
        // params.put("projectName", project.getProjectName());
        boolean isExist = projectDao.isProjectExist(params);
        boolean flag = false;
        if (isExist)
        {
            flag = projectDao.modProject(project);
        }
        else
        {
            log.error("该项目信息已经删除或不存在，项目信息无法修改，项目编号： " + project.getProjectId());
            throw new ITCException("3002");
        }

        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl modProjectInfo() method,return boolean");
        }
        return flag;
    }

    /**
     * @category 删除项目相关信息
     * @exception ITCException
     * @param projectId
     *            String
     * @return boolean
     */
    public boolean delProjectInfo(String projectId) throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl delProjectInfo() method,return boolean");
        }
        boolean isMeExt = projectDao.isMediumExist(projectId);
        boolean flag = false;
        if (!isMeExt)
        {
            flag = projectDao.delProject(projectId);
        }
        else
        {
            log.error("该项目下已有相关排期信息，项目信息无法删除，项目编号： " + projectId);
            throw new ITCException("3003");
        }
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl delProjectInfo() method,return boolean");
        }
        return flag;
    }

    /**
     * @category 查询项目列表数据
     * @param params
     *            Map<String,Object>,page PageBean
     * @return List<Project>
     * @exception ITCException
     */
    public List<Project> getProjectList(Map<String, Object> params,
            PageBean page) throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl getProjectList() method,return List<Project>");
        }
        List<Project> plist = null;
        plist = projectDao.getProjectList(params, page);
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl getProjectList() method,return List<Project>");
        }
        return plist;
    }

    /**
     * @category 获取项目下相关信息
     * @param project
     *            Project
     * @return Map<String,Object>
     * @exception ITCException
     */
    public Project getProjectRelateInfo(Map<String, Object> params)
            throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl getProjectRelateInfo() method,return Project");
        }
        Project pro = projectDao.getProjectRelateInfo(params);
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl getProjectRelateInfo() method,return Project");
        }
        return pro;
    }

    public String getProjCurStatus(String projectId) throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl getProjCurStatus() method,return String");
        }
        String status = projectDao.getProjCurStatus(projectId);
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl getProjCurStatus() method,return String");
        }
        return status;
    }

    /**
     * @category 获取项目下相关投放明细信息
     * @param projectId
     *            String
     * @return Map<String, Integer>
     * @exception ITCException
     */
    public Map<String, Integer> getCastCountByProjId(String projectId,
            String curStatus) throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl getCastCountByProjId() method,return Map<String, Integer>");
        }
        Map<String, Integer> countMap = projectDao.getCastCountByProjId(projectId,
                curStatus);
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl getCastCountByProjId() method,return Map<String, Integer>");
        }
        return countMap;
    }

    /**
     * @category 获取项目下相关投放明细信息
     * @param projectId
     *            String
     * @return Map<String,List<CastForProj>>
     * @exception ITCException
     */
    public Map<String, List<CastForProj>> getCastByProjId(String projectId,
            String curStatus) throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl getCastByProjId() method,return Map<String,List<CastForProj>>");
        }

        Map<String, List<CastForProj>> castMap = projectDao.getCastByProjId(projectId,
                curStatus);

        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl getCastByProjId() method,return Map<String,List<CastForProj>>");
        }
        return castMap;
    }

    /**
     * @category 获取项目下相关投放明细信息
     * @param projectId
     *            String
     * @return List<CastForProj>
     * @exception ITCException
     */
    public List<CastForProj> getCastArrangeByProjId(String projectId)
            throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl getCastArrangeByProjId() method,return List<CastDetail>");
        }
        List<CastForProj> arrangeList = projectDao.getCastArrangeByProjId(projectId);
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl getCastArrangeByProjId() method,return List<CastDetail>");
        }
        return arrangeList;
    }

    /**
     * @category 根据项目媒介ID删除投放明细信息
     * @param projMedId
     *            Integer
     * @return boolean
     * @exception ITCException
     */

    public boolean deleteCastInfo(Integer projMedId) throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl deleteCastInfo() method,return boolean");
        }
        boolean flag = projectDao.deleteCastInfo(projMedId);
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl deleteCastInfo() method,return boolean");
        }
        return flag;
    }

    /**
     * @category 根据项目媒介ID修改投放明细资源状态
     * @param proMedIds
     *            List<Integer>, status String
     * @return boolean
     * @exception ITCException
     */
    public boolean updateCastStatus(String status, List<Integer> proMedIds)
            throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl updateCastStatus() method,return boolean");
        }
        boolean flag = projectDao.updateCastStatus(status, proMedIds);
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl updateCastStatus() method,return boolean");
        }
        return flag;
    }

    public Integer getAmountByCur(Map<String, Object> params)
            throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl getAmountByCur() method,return Integer");
        }
        Integer amount = projectDao.getAmountByCur(params);
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl getAmountByCur() method,return Integer");
        }
        return amount;
    }

    public Integer getAmountByHis(Map<String, Object> params)
            throws ITCException
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectServiceImpl getAmountByHis() method,return Integer");
        }
        Integer amount = projectDao.getAmountByHis(params);
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectServiceImpl getAmountByHis() method,return Integer");
        }
        return amount;
    }

    public ProjectDao getProjectDao()
    {
        return projectDao;
    }

    public void setProjectDao(ProjectDao projectDao)
    {
        this.projectDao = projectDao;
    }

}
